'use strict';

var localVideo = document.getElementById("localVideo");
var pushBtn = document.getElementById("pushBtn");
var stopPushBtn = document.getElementById("stopPushBtn");

pushBtn.addEventListener("click", startPush);
stopPushBtn.addEventListener("click", stopPush);

var uid = $("#uid").val();
var streamName = $("#streamName").val();
var audio = $("#audio").val();
var video = $("#video").val();
var mode = $("#mode").val();
var isPli = $("#isPli").val();

var offer = "";
var pc;
const config = {};
var localStream;
var lastConnectStatus = "";

function startPush() {
    console.log("send push: /signaling/push");

    $.post("/signaling/push",
        { "uid": uid, "streamName": streamName, "audio": audio, "video": video, "isDtls": 1, "mode": mode, "isPli": isPli },
        function (data, textStatus) {
            console.log("push response: " + JSON.stringify(data));
            if ("success" == textStatus && 0 == data.errNo) {
                $("#tips1").html("<font color='blue'>推流请求成功!</font>");
                console.log("offer sdp: \n" + data.data.sdp);
                offer = data.data;
                pushStream();
            } else {
                $("#tips1").html("<font color='red'>推流请求失败!</font>");
            }
        },
        "json"
    );
}
function stopPush() {

    console.log("send stop push:/signaling/stoppush");
    localVideo.srcObject = null;
    if (localStream && localStream.getAudioTracks()) {
        localStream.getAudioTracks()[0].stop();
    }
    if (localStream && localStream.getVideoTracks()) {
        localStream.getVideoTracks()[0].stop();
    }
    if (pc) {
        pc.close()
        pc = null;
    }

    $("#tips1").html("");
    $("#tips2").html("");
    $("#tips3").html("");

    $.post("/signaling/stoppush",
        { "uid": uid, "streamName": streamName, "mode": mode },
        function (data, textStatus) {
            console.log("stoppush response: " + JSON.stringify(data));
            if ("success" == textStatus && 0 == data.errNo) {
                $("#tips1").html("<font color='blue'>停止推流请求成功!</font>");
            } else {
                $("#tips1").html("<font color='red'>停止推流请求失败!</font>");
            }
        },
        "json"
    );


}
function sendAnswer(answerSdp) {
    console.log("send answer: /signaling/sendanswer");

    $.post("/signaling/sendanswer",
        {
            "uid": uid, "streamName": streamName, "answer": answerSdp,
            "type": "push", "mode": mode
        },
        function (data, textStatus) {
            console.log(" send answer response:" + JSON.stringify(data));
            if ("success" == textStatus && 0 == data.errNo) {
                $("#tips3").html("<font color='blue'>Answer发送成功!</font>");
            } else {
                $("#tips3").html("<font color='red'>Answer发送失败!</font>");
            }
        },
        "json"
    );


}

function pushStream() {
    pc = new RTCPeerConnection(config);
    pc.onconnectionstatechange = function (e) {
        var state = "";
        if (lastConnectStatus != "") {
            state = lastConnectStatus + "->" + pc.iceConnectionState;
        } else {
            state = pc.iceConnectionState;
        }
        $("#tips2").html("链接状态:" + state);
        lastConnectStatus = pc.iceConnectionState;

    };

    pc.setRemoteDescription(offer).then(
        setRemoteDescriptionSuccess,
        setRemoteDescriptionError
    );
}
window.addEventListener("message", function (event) {
    if (event.origin != window.location.origin) {
        return;
    }

    if (event.data.type) {
        if (event.data.type == "SS_DIALOG_SUCCESS") {
            console.log("用户同意屏幕共享, streamId: " + event.data.streamId);
            startScreenStreamFrom(event.data.streamId);
        } else if (event.data.type == "SS_DIALOG_CANCEL") {
            console.log("用户取消屏幕共享");
        }
    }
});

function startScreenStreamFrom(streamId) {
    var constraints = {
        audio: false,
        video: {
            mandatory: {
                chromeMediaSource: "desktop",
                chromeMediaSourceId: streamId,
                maxWidth: window.screen.width,
                maxHeight: window.screen.height
            }
        }
    };

    navigator.mediaDevices.getUserMedia(constraints).then(
        handleSuccess).catch(handleError);
}

function handleSuccess(stream) {
    navigator.mediaDevices.getUserMedia({ audio: true }).then(
        function (audioStream) {
            stream.addTrack(audioStream.getAudioTracks()[0]);
            localVideo.srcObject = stream;
            localStream = stream;
            pc.addStream(stream);
            pc.createAnswer().then(
                createSessionDescriptionSuccess,
                createSessionDescriptionError
            );
        }
    ).catch(handleError);
}
function handleError(error) {
    console.log("get user media error:" + error);
}
function setRemoteDescriptionSuccess() {
    console.log("pc set remote description success!!!!!");
    console.log("request screen shared stream");

    // window.postMessage({ type: "SS_UI_REQUEST", text: "push" }, "*"); //弹出选择屏幕共享的窗口
    var constraints = {
        audio: false,
        video: true,

    };
    navigator.mediaDevices.getDisplayMedia(constraints).then(
        handleSuccess).catch(handleError);
}

function createSessionDescriptionSuccess(answer) {

    console.log("answer sdp:\n" + answer.sdp);
    console.log("pc set local sdp");
    pc.setLocalDescription(answer).then(
        setLocalDescriptionSuccess,
        setLocalDescriptionError
    );
    //发送answer 给服务器----
    sendAnswer(answer.sdp);
}

function setRemoteDescriptionError(error) {

    console.log("pc set remote descipriton error:" + error);
}
function createSessionDescriptionError(error) {

    console.log("pc create answer error\n" + error);


}

function setLocalDescriptionSuccess() {

    console.log("pc set local description success");
}


function setLocalDescriptionError(error) {
    console.log("pc set local description error : " + error);
}

